/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.unpack.file.global;

import cn.ac.iie.unpack.file.config.ConfigureUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.log4j.Logger;


/**
 * @author QJP
 */
public class GlobalParas {

    public static Logger LOG = Logger.getLogger(GlobalParas.class);

    //properties name
    public final static String LOG4_CONFIGURATION = "log4j.properties";
    public final static String DEFAULT_CONFIGURATION = "config.properties";

    //configuration.properties parameters
    public static String SOURCE_DIR;
    public static String SUS_UNTAR_DIR;
    public static String PARSER_DESTINATION_DIR;
    public static String DISTRI_DESTINATION_DIR;
    
    public static String SEND_CONSUMER_NAMESERVER;
    public static int SEND_MQ_MSG_NUM;
    public static int SEND_MQ_TIME_OUT;
    public static String[] MSGTYPES;
    public static String[] TOPICS;
    public static String[] DIRS;
    public static List<String[]> URLS=new ArrayList();
    public static List<List<String>> COLUMNS=new ArrayList();
    public static List<List<String>> TYPES=new ArrayList();
    
    
    public static int UNPACK_THREAD_NUM;
    public static List<Object>handle;
    
    public static ArrayBlockingQueue<String> FILE_QUEUE;
    public static int FILE_LIST_NUM;
    
    public static String FILE_SUFFIX;
    

    private static volatile GlobalParas instanceDefConf;

    public GlobalParas(String confPropertiesName) throws ConfigurationException {
        String confPropertiesPath = ClassLoader.getSystemClassLoader().getResource(confPropertiesName).getPath();
        ConfigureUtil configureUtil = new ConfigureUtil();
        FileBasedConfiguration configuration = configureUtil.initConfiguration(confPropertiesPath);
        if (confPropertiesName.equals(DEFAULT_CONFIGURATION)) {
            initConf(configuration);
        }
    }

    public static GlobalParas getIstance(String confPropertiesName) throws ConfigurationException {
        if (confPropertiesName.equals(DEFAULT_CONFIGURATION)) {
            if (instanceDefConf == null) {
                synchronized (GlobalParas.class) {
                    if (instanceDefConf == null) {
                        instanceDefConf = new GlobalParas(confPropertiesName);
                    }
                }
            }
            return instanceDefConf;
        } else {
            LOG.error("Could Not Find Configuration: " + confPropertiesName);
            return null;
        }
    } 

    public static void initConf(FileBasedConfiguration configuration) {
        SOURCE_DIR=configuration.getString("source.dir");
        SUS_UNTAR_DIR=configuration.getString("sus.untar.dir");
        PARSER_DESTINATION_DIR=configuration.getString("parser.destination.dir");
        DISTRI_DESTINATION_DIR=configuration.getString("distri.destination.dir");
        
        SEND_CONSUMER_NAMESERVER=configuration.getString("send.consumer.nameserver");
        SEND_MQ_MSG_NUM=configuration.getInt("send.mq.msg.num");
        SEND_MQ_TIME_OUT=configuration.getInt("send.mq.time.out");
        MSGTYPES=configuration.getString("msg.types.name").split("[|]");
        TOPICS=configuration.getString("send.consumer.topic").split("[|]");
        for(int i=0;i<MSGTYPES.length;i++){
            List list1=new ArrayList();
            List list2=new ArrayList();
            
            for(String msg:configuration.getString("msg.type."+MSGTYPES[i]).split("[|]")){
                String[] col_ty=msg.split(":");
                list1.add(col_ty[0]);
                list2.add(col_ty[1]);
            };
            COLUMNS.add(list1);
            TYPES.add(list2);
        }
            LOG.info(configuration.getString("msg.types.url"));
        for(String msg:configuration.getString("msg.types.url").split("[:]")){
            URLS.add(msg.split("[|]"));
            LOG.info(Arrays.toString(msg.split("[|]")));
        };
        DIRS=configuration.getString("msg.types.dir").split("[|]");
        UNPACK_THREAD_NUM=configuration.getInt("unpack.thread.num");
        handle=configuration.getList("handle");
        FILE_LIST_NUM=configuration.getInt("file.list.num");
        FILE_QUEUE=new ArrayBlockingQueue<>(FILE_LIST_NUM);
        
        FILE_SUFFIX=configuration.getString("file.suffix");
    }
}
